package edu.sctu.lab03.dao;




import edu.sctu.lab03.DbUtil;
import edu.sctu.lab03.User;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;


public class UserDao {

    DbUtil dbUtil = new DbUtil();


    public User login(Connection connection, User user) throws Exception {
        User resultUser = null;
        String sql = "select * from t_users where userName=? and password=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        preparedStatement.setString(1, user.getName());
        preparedStatement.setString(2, user.getPassword());

        ResultSet resultSet = preparedStatement.executeQuery();

        if (resultSet.next()) {
            resultUser = new User();
            resultUser.setId(resultSet.getInt("id"));
            resultUser.setName(resultSet.getString("userName"));
            resultUser.setPassword(resultSet.getString("password"));

        }
        return resultUser;
    }

    public void register(Connection connection, User user) throws SQLException {

        String sql = "INSERT INTO t_users(userName,password) VALUES (?,?)";


        PreparedStatement preparedStatement = connection.prepareStatement(sql); //预编译SQL，减少sql执行


        preparedStatement.setString(1, user.getName());
        preparedStatement.setString(2, user.getPassword());

        //执行
        preparedStatement.execute();
    }


    public boolean isExist(Connection connection, User user) throws SQLException {

        User resultUser = null;
        String sql = "select * from t_users where userName=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);


        preparedStatement.setString(1, user.getName());


        ResultSet resultSet = preparedStatement.executeQuery();

        if (resultSet.next()) {
            return false;
        } else {
            return true;
        }

    }

    public void update(Connection connection, User user) throws SQLException {
        String sql = "UPDATE t_users SET userName =?,password=? WHERE id=?";


        PreparedStatement preparedStatement = connection.prepareStatement(sql); //预编译SQL，减少sql执行

        preparedStatement.setString(1, user.getName());
        preparedStatement.setString(2, user.getPassword());
        preparedStatement.setString(3, String.valueOf(user.getId()));


        preparedStatement.execute();
    }


    public void delete(Connection connection, User user) throws SQLException {

        String sql = "delete from t_users where id=?";


        PreparedStatement ptmt = connection.prepareStatement(sql);


        ptmt.setInt(1, Integer.parseInt(String.valueOf(user.getId())));

        ptmt.execute();
    }


    public List<User> getUserList() {
        List<User> userList = new ArrayList<User>();

        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;


        try {
            String sql = "select * from t_users";
            connection = dbUtil.getCon();
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);


            while (resultSet.next()) {
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String password = resultSet.getString(3);


                User user = new User();
                user.setId(id);
                user.setName(name);
                user.setPassword(password);

                userList.add(user);

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭
            try {
                resultSet.close();
                statement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

        return userList;
    }
}
